<template>
  <div class="tab-pane-wrapper">
    <el-form class="form-search" :model="form" inline>
      <label class="btn-nav-bg stripe-btn" @click="handleAdd">新 增</label>
    </el-form>
    <page-table
      :tableData="tableData"
      :tableLabel="colSData"
      :showSelection="false"
      :pager="pager"
      showIndex
      :table-height="tableHeight"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @handleButton="handleButton(arguments)"
    ></page-table>
    <pub-dialog
      :title="dialogTitle"
      type="big"
      top="15vh"
      :sureBtnLoading="loading"
      :dialogVisible="dialogVisible"
      dialogVisableRef="dialogSateVisible"
      @switchVisable="switchVisable"
      @dialogSubmit="dialogSubmit"
      @dialogCancel="dialogCancel"
      btnType="3"
    >
      <el-form
        class="form-search"
        label-width="90px"
        :rules="rules"
        ref="formData"
        :model="formData"
      >
        <el-form-item label="执行标准" prop="standard">
          <el-input placeholder="请输入" v-model="formData.standard"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="formData.state">
            <el-radio :label="0">健康</el-radio>
            <el-radio :label="1">异常</el-radio>
            <el-radio :label="2">故障</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="缺陷定位" prop="flaw">
          <el-input placeholder="请输入" v-model="formData.flaw"></el-input>
        </el-form-item>
        <el-form-item label="处理脚本" prop="script">
          <el-input
            placeholder="请输入"
            type="textarea"
            :rows="3"
            v-model="formData.script"
          ></el-input>
        </el-form-item>
        <el-form-item label="执行顺序" prop="sort">
          <el-input
            placeholder="请输入"
            v-model.number="formData.sort"
          ></el-input>
        </el-form-item>
        <el-form-item label="" v-if="formData.id">
          <p>历史脚本</p>
          <div class="detail">
            <p
              @click="handleRecord(item)"
              v-for="(item, index) in formData.record"
              :key="index"
            >
              {{ item }}
            </p>
          </div>
        </el-form-item>
      </el-form>
    </pub-dialog>
  </div>
</template>
<script>
import PageTable from "@/components/PageTable";
import pubDialog from "@/components/pubDialog.vue";
import {
  xjAssessValueDelete,
  xjAssessValueList,
  xjAssessValueUpdate,
  xjAssessValueAdd,
} from "@/api/pointMaintain";
export default {
  name: "sonModuleEva",
  props: {
    siteId: {
      type: String,
      default: "",
    },
  },
  components: {
    pubDialog,
    PageTable,
  },
  data() {
    return {
      form: {},
      tableData: [],
      colSData: [
        { label: "执行标准", param: "standard" },
        { label: "状态", param: "stateDesc" },
        { label: "缺陷定位", param: "flaw" },
        { label: "处理脚本", param: "script" },
        { label: "执行顺序", param: "sort" },
        {
          label: "操作",
          param: "operate",
          width: 300,
          operate: [
            {
              label: "编辑",
              click: "edit",
              type: "text",
            },
            {
              label: "删除",
              click: "remove",
              type: "text",
            },
          ],
        },
      ],
      pager: {
        pageSize: 10, // 每页数据量
        currentPage: 1, // 分页  当前页
        total: 0, //列表总数
      },
      tableHeight: 400,
      formData: {
        standard: "",
        script: "",
        state: "",
        flaw: "",
        sort: "",
        record: [],
      },
      rules: {},
      dialogVisible: false,
      loading: false,
      dialogTitle: "添加",
    };
  },
  created() {
    this.calcHeight();
    this.getList();
  },
  mounted() {},
  methods: {
    calcHeight() {
      this.tableHeight = window.innerHeight - 226;
    },
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.getList();
    },
    handleAdd() {
      this.dialogVisible = true;
      this.dialogTitle = "添加";
      this.formData = {
        standard: "",
        script: "",
        state: "",
        flaw: "",
        sort: "",
        record: [],
      };
    },
    handleButton(method, row, index) {
      if (method[0] === "remove") {
        xjAssessValueDelete({ id: method[1].id }).then((res) => {
          if (res.code == 20000) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        this.dialogVisible = true;
        this.dialogTitle = "编辑";
        for (let key in this.formData) {
          this.formData[key] = method[1][key];
        }
        this.formData.id = method[1].id;
      }
    },

    async getList() {
      let params = {
        siteId: this.siteId,
      };
      let result = await xjAssessValueList(params);
      if (result.code == 20000) {
        this.tableData = result.data;
        this.tableData.forEach((item) => {
          if (item.state == 0) {
            item.stateDesc = "健康";
          } else if (item.state == 1) {
            item.stateDesc = "异常";
          } else if (item.state == 2) {
            item.stateDesc = "故障";
          }
        });
      }
    },
    switchVisable(visable, ref) {
      this[ref] = visable;
    },
    dialogCancel() {
      this.dialogVisible = false;
    },
    handleRecord(item) {
      this.formData.script = item;
    },
    // 保存添加或者编辑测点
    dialogSubmit() {
      this.loading = true;
      let params = { ...this.formData };
      params.siteId = this.siteId;
      params.state = params.state.toString();
      let API = params.id ? xjAssessValueUpdate : xjAssessValueAdd;
      API(params)
        .then((res) => {
          if (res.code == 20000) {
            this.dialogVisible = false;
            this.getList();
            this.$message.success("保存成功");
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
          return err;
        });
    },
  },
};
</script>
<style scoped lang="less">
::v-deep .form-search .el-form-item {
  margin-bottom: 22px;
}
::v-deep .el-dialog__body {
  max-height: 60vh;
  overflow-y: hidden;
  .el-form.form-search {
    height: 100%;
    overflow-y: hidden;
  }
}
.tab-pane-wrapper {
  height: 100%;
  width: 100%;
  padding: 20px;
  background-color: #202f3e;
  .form-search {
    margin-bottom: 16px;
    p {
      font-size: 16px;
      font-weight: bold;
      line-height: 32px;
      text-align: center;
      color: #c6d3ec;
      border: 1px solid #40e5f0;
    }
    .detail {
      height: calc(50vh - 334px);
      overflow-y: auto;
      p {
        font-weight: normal;
        font-size: 12px;
        border-bottom: 0;
        cursor: pointer;

        &:last-child {
          border-bottom: 1px solid #40e5f0;
        }
      }
    }
  }
}
</style>
